
***** 3 main outcomes for pooling
***   Weighted as in main table



* Family: ASB
index_maker , indexname(fam_asb_ew_z) indexlab("Antisocial Behaviors") inputvars( ///
         badreact_resc badadvice_resc hardjokes_resc baddestroy_resc cheat_resc ///
		troublesome_resc frustrated_resc threaten_resc takethings_resc selfdiscipline_resc  ///
		asb_reactlose_resc asb_reacthit_resc asb_reactdefend_resc asb_reacttease_resc  ///
		asb_proontop_resc asb_provandalize_resc asb_procompetitor_resc asb_prophysforce_resc ///
		asb_procuss_resc fightbigneigh fightweapon fightbigleader fightbigpolice fine carryweapon ///
		drugssellever stealtakethingsx stealmistakecorrx stealscrapex stealpickpocketx stealscamx ///
		stealblackdeedx stealmuggerx stealarmedrobx arrested ///
		maccuse_resc mthreat_resc minsult_resc mhit_resc ) ///
              suffix(e) operation(mean) recenter


*Censoring is doing at one level higher, so to keep the names harmonic this would be the same.
gen cens_fam_asb_ew_z_e =  fam_asb_ew_z_e


* Family: Econ (Weighting is the same as main index
gen fam_econ_ew_z_e =  fam_econ_e

* Family: Identity
index_maker , indexname(fam_identity_ew_z) indexlab("Identity and values") inputvars(nvstrangerrob nvowetakesthg nvknowncriminal nvstorekeeperpunish_resc nvkillstore_resc ///
							nvbeatwoman nvdestroyproperty nvstealfriendsgirl nvcorruptleaderbeaten nvbeatwife nvgirlrape  ///
							attstchinamanbeat attstblackmoney attstnightops_resc attsttaxiwallet ///
							attstmoneydroptake attstfamilylec attstmoneybagtake attstmoneyhangtake ///
							attstroomvaluablestake attselfdrugsell attstknownindiv attstifreturn ///
							pvhypdemstpaid  pvhyppalpaid pvhyppalpaid200ld ///
							pvhypdemstpaidwcheat pvhyppalpaidwcheat ///
							pvhyppalpaid200ldwcheat ///
						    pvtalkdemstdum pvtalkpaldum  pvtalkdemstwcheatdum ///
						    clothescond_resc shoecond_resc breath_resc cleanface_resc ///
							hair_resc fingernails_resc ///
							groups  ///
							groupleadindex ///
							publicgoodcontrnumb  ///
							trustindex) ///
              suffix(e) operation(mean) recenter

gen timepref_ew_z_e =  timepref_e

* Self-Control
index_maker , indexname(selfcontrolnolo_ew_z) indexlab("Self-control skills") inputvars(  ///
									bimpls_buy_resc bimpls_act_resc bimpls_talk_resc bimpls_squirm_resc bimpls_concentrate_resc  ///
						           	bimpls_present_resc bimpls_control_resc time_decl_regretspend_resc ///
						           	cnsc_prepare_resc cnsc_details_resc cnsc_doitright_resc cnsc_plans_resc  ///
						           	cnsc_getby_resc cnsc_work_resc cnsc_complete_resc cnsc_shirk_resc  ///
						           	prsv_overcome_resc prsv_ambitious_resc prsv_discourage_resc prsv_achieveimp_resc  ///
						           	prsv_driven_resc prsv_changeplan_resc prsv_achievenoimp_resc ///
						           	bas_goallout_resc bas_noholds_resc bas_doforfun_resc bas_keepatit_resc bas_feelexcite_resc  ///
						           	bas_seeopp_resc bas_feelstrongly_resc bas_wincontest_resc) ///
              suffix(e) operation(mean) recenter


* Mental Health
index_maker , indexname(fam_mental_ew_z) indexlab("Positive self-regard/mental health") inputvars( ///
									nrtc_sad_resc nrtc_worry_resc nrtc_threaten_resc nrtc_offend_resc  ///
					 				nrtc_stress_resc nrtc_disturb_resc nrtc_relax_resc nrtc_bother_resc ///
					 				lc_choicesfuture_resc lc_nocontrol_resc lc_bizluck_resc lc_tryhard_resc  ///
					 				lc_planswork_resc lc_badluck_resc lc_peoplestreets_resc lc_success_resc ///
					 				se_satisfied_resc  se_useless_resc se_fail_resc se_respect_resc  ///
					 				se_goodperson_resc se_good4nothing_resc se_bizwell_resc se_notproud_resc ///
					 				baddo_resc badhear_resc badsad_resc badtired_resc badeat_resc badimportant_resc ///
					 				thinkbad_resc baddream_resc badagain_resc badbody_resc  badaway_resc   ///
					 				badspoiled_resc badtalk_resc  badfrust_resc badspirit_resc baddry_resc badsat_resc badburn_resc ///
					 				happiness satisfaction health ///
					 				laddernow ladderrich ladderresp ladderpower ladderaccess ) ///
              suffix(e) operation(mean) recenter


* Family Networks
index_maker , indexname(fam_network_ew_z) indexlab("Quality Social Networks") inputvars( ///
									peerschool peercommmtg peerchurch peerwork peerbusiness peersave ///
									peertrust peershare peercheer peerguard  ///
					    	  		peerbeg_resc peerdrunk_resc peerdrugs_resc peerpickpocket_resc ///
					    	  		peerburglary_resc  peerrobbers_resc peergamble_resc peerexcom_resc ///
					    	  		peercomdr_resc peersmallpal_resc peerbigpal_resc ///
					    	  		famseeoften_resc famconcern_resc famadvise_resc famhelp_resc ///
					    	  		peercomdr relcomm jobcomm repcomm ///
					    	  		ptrnjob ptrnbiz ptrnfood ptrnschl  ptrnhsing ) ///
              suffix(e) operation(mean) recenter

gen fam_subabuse_ew_z_e =  fam_subabuse_e



** Covariance weights
index_cov_maker , indexname(fam_asb) indexlab("Antisocial Behaviors") inputvars(asbhostil_z_p cens_disputes_all_z_p carryweapon drugssellever stealnb arrested domabuse_z_p ) ///
               suffix(tyav) operation(mean) recenter
index_cov_maker , indexname(cens_fam_asb) indexlab("Antisocial Behaviors") inputvars(asbhostil_z_p cens_disputes_all_z_p carryweapon drugssellever cens_stealnb arrested domabuse_z_p ) ///
               suffix(tyav) operation(mean) recenter


index_cov_maker , indexname(fam_econ) indexlab("Economic Performance") inputvars(profitsump99avg7d wealth_indexstd cstot2wusd homeless houravg7d savstockp99) ///
               suffix(tyav) operation(mean) recenter rescale(homeless)
index_cov_maker , indexname(fam_identity) indexlab("Identity and values") inputvars(attviolence attcriminality politicalviol appearanceindex prosocialindex) ///
              suffix(tyav) operation(mean) recenter rescale(attviolence attcriminality politicalviol)
index_cov_maker , indexname(fam_network) indexlab("Quality of social networks") inputvars(peerqualitystd famsupportindex patronlvl rel_commanders) ///
              suffix(tyav) operation(mean) recenter
index_cov_maker , indexname(fam_mental) indexlab("Positive self-regard/mental health") inputvars(neuroticstd selfesteemstd wellbeing locuscontrstd depression distress) ///
              suffix(tyav) operation(mean) recenter rescale(neuroticstd depression distress)
index_cov_maker , indexname(timepref) indexlab("Forward-looking time preferences") inputvars(timedecl_p patient_game_real patient_game_decl patient_rate_dsct timedecl_t incon_game_real incon_game_decl incon_rate_dsct) ///
              suffix(tyav) operation(mean) recenter rescale(incon_game_real incon_game_decl incon_rate_dsct)
index_cov_maker , indexname(selfcontrolnolo) indexlab("Self-control skills") inputvars(impulsivestd_resc rewardrespstd_resc conscientiousstd gritstd) ///
              suffix(tyav) operation(mean) recenter
index_cov_maker , indexname(fam_subabuse) indexlab("Substance Abuse") inputvars(drinkboozeever grassever harddrugsever) ///
              suffix(tyav) operation(mean) recenter


** Covariance weights
index_cov_maker , indexname(fam_asb) indexlab("Antisocial Behaviors") inputvars(asbhostil_z_p cens_disputes_all_z_p carryweapon drugssellever stealnb arrested domabuse_z_p ) ///
               suffix(ltav) operation(mean) recenter
index_cov_maker , indexname(cens_fam_asb) indexlab("Antisocial Behaviors") inputvars(asbhostil_z_p cens_disputes_all_z_p carryweapon drugssellever cens_stealnb arrested domabuse_z_p ) ///
               suffix(ltav) operation(mean) recenter


index_cov_maker , indexname(fam_econ) indexlab("Economic Performance") inputvars(profitsump99avg7d wealth_indexstd cstot2wusd homeless houravg7d savstockp99) ///
               suffix(ltav) operation(mean) recenter rescale(homeless)
index_cov_maker , indexname(fam_identity) indexlab("Identity and values") inputvars(attviolence attcriminality politicalviol appearanceindex prosocialindex) ///
              suffix(ltav) operation(mean) recenter rescale(attviolence attcriminality politicalviol)
index_cov_maker , indexname(fam_network) indexlab("Quality of social networks") inputvars(peerqualitystd famsupportindex patronlvl rel_commanders) ///
              suffix(ltav) operation(mean) recenter
index_cov_maker , indexname(fam_mental) indexlab("Positive self-regard/mental health") inputvars(neuroticstd selfesteemstd wellbeing locuscontrstd depression distress) ///
              suffix(ltav) operation(mean) recenter rescale(neuroticstd depression distress)
index_cov_maker , indexname(timepref) indexlab("Forward-looking time preferences") inputvars(timedecl_p patient_game_real patient_game_decl patient_rate_dsct timedecl_t incon_game_real incon_game_decl incon_rate_dsct) ///
              suffix(ltav) operation(mean) recenter rescale(incon_game_real incon_game_decl incon_rate_dsct)
index_cov_maker , indexname(selfcontrolnolo) indexlab("Self-control skills") inputvars(impulsivestd_resc rewardrespstd_resc conscientiousstd gritstd) ///
              suffix(ltav) operation(mean) recenter
index_cov_maker , indexname(fam_subabuse) indexlab("Substance Abuse") inputvars(drinkboozeever grassever harddrugsever) ///
              suffix(ltav) operation(mean) recenter


** Covariance weights

index_cov_maker , indexname(fam_asb) indexlab("Antisocial Behaviors") inputvars(asbhostil_z_p cens_disputes_all_z_p carryweapon drugssellever stealnb arrested domabuse_z_p ) ///
               suffix(stav) operation(mean) recenter
index_cov_maker , indexname(cens_fam_asb) indexlab("Antisocial Behaviors") inputvars(asbhostil_z_p cens_disputes_all_z_p carryweapon drugssellever cens_stealnb arrested domabuse_z_p ) ///
               suffix(stav) operation(mean) recenter


index_cov_maker , indexname(fam_econ) indexlab("Economic Performance") inputvars(profitsump99avg7d wealth_indexstd cstot2wusd homeless houravg7d savstockp99) ///
               suffix(stav) operation(mean) recenter rescale(homeless)
index_cov_maker , indexname(fam_identity) indexlab("Identity and values") inputvars(attviolence attcriminality politicalviol appearanceindex ) /// does not have prosocial index (was not calculated in st)
              suffix(stav) operation(mean) recenter rescale(attviolence attcriminality politicalviol)
index_cov_maker , indexname(fam_network) indexlab("Quality of social networks") inputvars(peerqualitystd famsupportindex patronlvl rel_commanders) ///
              suffix(stav) operation(mean) recenter
index_cov_maker , indexname(fam_mental) indexlab("Positive self-regard/mental health") inputvars(neuroticstd selfesteemstd wellbeing locuscontrstd depression distress) ///
              suffix(stav) operation(mean) recenter rescale(neuroticstd depression distress)
index_cov_maker , indexname(timepref) indexlab("Forward-looking time preferences") inputvars(timedecl_p patient_game_real patient_game_decl patient_rate_dsct timedecl_t incon_game_real incon_game_decl incon_rate_dsct) ///
              suffix(stav) operation(mean) recenter rescale(incon_game_real incon_game_decl incon_rate_dsct)
index_cov_maker , indexname(selfcontrolnolo) indexlab("Self-control skills") inputvars(impulsivestd_resc rewardrespstd_resc conscientiousstd gritstd) ///
              suffix(stav) operation(mean) recenter
index_cov_maker , indexname(fam_subabuse) indexlab("Substance Abuse") inputvars(drinkboozeever grassever harddrugsever) ///
              suffix(stav) operation(mean) recenter
/*

*****   Same weight for each component
	foreach p in e {

	* Family: ASB
		foreach v in badreact_resc_e badadvice_resc_e hardjokes_resc_e baddestroy_resc_e cheat_resc_e ///
		troublesome_resc_e frustrated_resc_e threaten_resc_e takethings_resc_e selfdiscipline_resc_e  ///
		asb_reactlose_resc_e asb_reacthit_resc_e asb_reactdefend_resc_e asb_reacttease_resc_e  ///
		asb_proontop_resc_e asb_provandalize_resc_e asb_procompetitor_resc_e asb_prophysforce_resc_e ///
		asb_procuss_resc_e fightbigneigh_e fightweapon_e fightbigleader_e fightbigpolice_e fine_e carryweapon_e ///
		drugssellever_e stealtakethingsx_e stealmistakecorrx_e stealscrapex_e stealpickpocketx_e stealscamx_e ///
		stealblackdeedx_e stealmuggerx_e stealarmedrobx_e arrested_e maccuse_resc_e mthreat_resc_e minsult_resc_e mhit_resc_e  {
			cap sum `v'
			cap gen BB_`v'_std = (`v' - r(mean))/r(sd)
		}
		egen fam_asb_ew_z_`p' = rowmean( BB_*)
		qui sum fam_asb_ew_z_`p'
		qui replace fam_asb_ew_z_`p' = (fam_asb_ew_z_`p' - r(mean))/r(sd)
		la var fam_asb_ew_z_`p' "Family: ASB, equally weighted all components"
		drop BB_*

	* Family: Econ (Weighting is the same as main index
		gen fam_econ_ew_z_`p' =  fam_econ_`p'

	* Family: Mechanisms

	 foreach x in distress depression neuroticstd selfesteemstd ///
		appearanceindex wellbeing locuscontrstd attviolence ///
		attcriminality politicalviol prosocialindex ///
		timepref_p timepref_t impulsivestd  conscientiousstd gritstd rewardrespstd ///
		drinkboozeever grassever harddrugsever peerqualitystd famsupportindex rel_commanders patronlvl {
			sum `x'_`p'
			gen Z_`x' = (`x'_`p'-r(mean))/r(sd)
			}
		foreach x in distress depression neuroticstd  attviolence ///
		attcriminality politicalviol impulsivestd rewardrespstd drinkboozeever grassever harddrugsever {
			replace Z_`x' = - Z_`x'
		}

		egen mech_allexceptef_ew_z_`p' = rowmean( Z_*)
		qui sum mech_allexceptef_ew_z_`p'
		qui replace mech_allexceptef_ew_z_`p' = (mech_allexceptef_`p' - r(mean))/r(sd)
		la var mech_allexceptef_ew_z_`p' "Family: mechanisms"
			drop Z_*

	}



// i. COVARIANCE WEIGHTS

* Create Covariance-Weighted Index for Average
local index "fam_asb fam_econ mech_allexceptef"
local indexlist_fam_asb "disputes_all drugssellever stealnb arrested asbhostil carryweapon domabuse_z"
local indexlist_fam_econ "cstot2wusd profitsump99avg7d wealth_indexstd savstockp99 houravg7d homeless"
local indexlist_mech_allexceptef "timepref selfcontrolnolo fam_identity fam_mental subabuse fam_network"

foreach y in `index' {

	foreach term in stav ltav tyav{

	local indexlist_`y'_Z
	local num : word count `indexlist_`y''

	* Standardize All Variables
	foreach q in `indexlist_`y'' {
		qui sum `q'_`term'
		gen Z_`q' = (`q'_`term'-r(mean))/r(sd)
		local indexlist_`y'_Z `indexlist_`y'_Z' Z_`q'
	}

	* Flip order
	foreach var in homeless subabuse  {
		cap replace Z_`var' = -Z_`var'
		}


	* Get Covariance Matrix
	corr `indexlist_`y'_Z' if inlist(round,1,2,5,6,7,8), cov
	matrix a = r(C)
	drop Z_*

	* Inverse
	matrix b = inv(a)

	* Multiply by Column Vector of 1's to get row sum
	matrix X1 = J(`num',1,1)
	matrix B1 = b*X1
	matrix list B1

	* Retrieve variable weights
	local i = 0
	local `y'_w_sum = 0
	foreach v in `indexlist_`y'' {
		local ++i
		local `y'_`i' = B1[`i',1]
	* Total Weight sum
		local `y'_w_sum = ``y'_w_sum'+ ``y'_`i''
	* Product-Sum
		qui gen cov_w_`i' = `v'_`term' * ``y'_`i''

	if "`v'" == " homeless" | "`v'" == "subabuse" {
			replace cov_w_`i' = -cov_w_`i'
		}
	}


	* Sum of Product-Sums
	egen `y'_cov_sum = rowmean(cov_w_*)

	* Standardize by sum of all weights
	qui gen `y'_cov_`term' = `y'_cov_sum/``y'_w_sum'
	cap local lab: var label `y'_e
	*label var `y'_cov_`term' "Cov-Index: `lab'"
	drop cov_w_* *_cov_sum

	* Normalize
	qui sum `y'_cov_`term'
	replace `y'_cov_`term' = (`y'_cov_`term'-r(mean))/r(sd)
	** This creates the Missings Filled in Index **
	cap label var `y'_cov_`term' "Cov-Index, Avg of Avg, Z: `lab'"

	}
}



* Create Covariance-Weighted Index for Average
local index "fam_asb"
local indexlist_fam_asb "disputes_all drugssellever stealnb arrested asbhostil carryweapon "

foreach y in `index' {

	foreach term in stav ltav tyav {

	local indexlist_`y'_Z
	local num : word count `indexlist_`y''

	* Standardize All Variables
	foreach q in `indexlist_`y'' {
		qui sum `q'_`term'
		gen Z_`q' = (`q'_`term'-r(mean))/r(sd)
		local indexlist_`y'_Z `indexlist_`y'_Z' Z_`q'
	}

	* Get Covariance Matrix
	corr `indexlist_`y'_Z' if inlist(round,1,2,5,6,7,8), cov
	matrix a = r(C)
	drop Z_*

	* Inverse
	matrix b = inv(a)

	* Multiply by Column Vector of 1's to get row sum
	matrix X1 = J(`num',1,1)
	matrix B1 = b*X1
	matrix list B1

	* Retrieve variable weights
	local i = 0
	local `y'_w_sum = 0
	foreach v in `indexlist_`y'' {
		local ++i
		local `y'_`i' = B1[`i',1]
	* Total Weight sum
		local `y'_w_sum = ``y'_w_sum'+ ``y'_`i''
	* Product-Sum
		qui gen cov_w_`i' = `v'_`term' * ``y'_`i''

	foreach l in `switch' {
		if "`v'" == "`l'" {
		replace cov_w_`i' = -cov_w_`i'
		}
	}
	}

	* Sum of Product-Sums
	egen `y'_cov_sum = rowmean(cov_w_*)

	* Standardize by sum of all weights
	qui gen `y'_cov2_`term' = `y'_cov_sum/``y'_w_sum'
	local lab: var label `y'_e
	drop cov_w_* *_cov_sum

	* Normalize
	qui sum `y'_cov2_`term'
	replace `y'_cov2_`term' = (`y'_cov2_`term'-r(mean))/r(sd)
	label var `y'_cov2_`term' "Cov-Index, Avg (no domestic violence), Z: `lab'"

	}
	}


/*

* Family: Econ
		foreach v in profitsump99avg7d wealth_indexstd cstot2wusd ///
		savstockp99 homeless bizexptotp99 bizstocktotp99 houravg7d {
				qui sum `v'_`p'
				qui gen Z_`v'_std_`p' = (`v'_`p'-r(mean))/r(sd)
					     cap label var Z_`v'_std_`p' `"`: variable label `v'_`p''"'
				}
		foreach v in homeless {
			qui replace Z_`v'_std_`p' = -Z_`v'_std_`p'
		}

		egen fam_econ_`p' = rowmean( Z_*)
		qui sum fam_econ_`p'
		*qui replace fam_econ_`p' = (fam_econ_`p' - r(mean))/r(sd)
		la var fam_econ_`p' "Family: econ"
		rename (Z_*) (*)


foreach p in e {

	* Family: ASB
		foreach v in asbhostil disputes_all_z carryweapon drugssellever 	///
		stealnb arrested domabuse_z {
			cap sum `v'_`p'
			cap gen BB_`v'_std_`p' = (`v'_`p' - r(mean))/r(sd)
			 	 cap label var BB_`v'_std_`p' `"`: variable label `v'_`p''"'

		}
		egen fam_asb_`p' = rowmean( BB_*)
		qui sum fam_asb_`p'
		*qui replace fam_asb_`p' = (fam_asb_`p' - r(mean))/r(sd)
		la var fam_asb_`p' "Family: ASB"
		rename (BB_*) (*)



	* Family: mental

	 foreach x in distress depression neuroticstd selfesteemstd ///
		 wellbeing locuscontrstd {
			sum `x'_`p'
			gen Z_`x'_std_`p' = (`x'_`p'-r(mean))/r(sd)
    	 	     cap label var Z_`x'_std_`p' `"`: variable label `x'_`p''"'
			}
		foreach x in distress depression neuroticstd  {
			replace Z_`x'_std_`p' = - Z_`x'_std_`p'
		}
		egen fam_mental_`p'_std_`p' = rowmean(Z_*)
		sum fam_mental_`p'
		*replace fam_mental_`p' = (fam_mental_`p'-r(mean))/r(sd)
		la var fam_mental_`p' "Family index: mental health"
		rename (Z_*) (*)


	* Family: Identity
	foreach x in attviolencestd attcriminalitystd politicalviolstd appearanceindex prosocialindex {
				qui sum `x'_`p'
				qui gen Z_`x'_std_`p' = (`x'_`p'-r(mean))/r(sd)
					     cap label var Z_`x'_std_`p' `"`: variable label `x'_`p''"'
				}
		foreach x in attviolencestd attcriminalitystd politicalviolstd {
			qui replace Z_`x'_std_`p' = -Z_`x'_std_`p'
		}

		egen fam_identity_`p' = rowmean(Z_*)
		qui sum fam_identity_`p'
		*qui replace fam_identity_`p' = (fam_identity_`p'-r(mean))/r(sd)
		la var fam_identity_`p' "Family index: income"
		rename (Z_*) (*)

			* 5. Substance abuse
		gen subabuse_`p' = subabuse_`p'

		* 6. Social network
		gen fam_network_`p' = socialnetwork_`p'





	* Family: mechanisms
		foreach v in timepref selfcontrolnolo fam_identity fam_mental subabuse socialnetwork {
				qui sum `v'_`p'
				qui gen Z_`v'_std_`p' = (`v'_`p'-r(mean))/r(sd)
				}
		foreach v in subabuse {
			qui replace Z_`v'_std_`p' = -Z_`v'_std_`p'
		}

		egen mech_allexceptef_`p' = rowmean( Z_*)
		qui sum mech_allexceptef_`p'
		*qui replace mech_allexceptef_`p' = (mech_allexceptef_`p' - r(mean))/r(sd)
		la var mech_allexceptef_`p' "Family: mechanisms"
		}
        drop Z_*
